package com.finace.money.ui.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

/**
 * Created by ZHP on 2017/6/30.
 */

public class FragmentFactory {

    public static final int TAB_HOME = 0;
    public static final int TAB_STATUS = 1;
    public static final int TAB_HAVE = 2;
    public static final int TAB_NEWS = 3;
    public static final int TAB_MINE = 4;

    public static Fragment createFragment(FragmentManager fragmentManager, int position, int showLayoutId) {
        // 从内存获取到fragment
        Fragment fragment = fragmentManager.findFragmentByTag(position+"");

        if (fragment == null) {
            switch (position) {
                case TAB_HOME:
                    fragment = new MainHomeFragment();
                    break;
                case TAB_STATUS:
                    fragment = new MainStatusFragment();
                    break;
                case TAB_HAVE:
                    fragment = new MainHaveFragment();
                    break;
                case TAB_NEWS:
                    fragment = new MainNewsFragment();
                    break;
                case TAB_MINE:
                    fragment = new MainMineFragment();
                    break;
            }
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.add(showLayoutId,fragment,position+"");
//            transaction.commit();
            transaction.commitAllowingStateLoss();
        }

        return fragment;
    }

    /**
     *
     * @param fragmentManager     事务管理器不可使用全局变量
     * @param showPosition          展示对应位置Fragment
     */
    public static Fragment changeFragment(FragmentManager fragmentManager, int showPosition, int showLayoutId) {
        //创建当前显示的Fragment
        Fragment fragment = FragmentFactory.createFragment(fragmentManager,showPosition,showLayoutId);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //隐藏不需要显示的view
        Fragment itemFragment;
        int[] keys ={TAB_HOME,TAB_STATUS,TAB_HAVE,TAB_NEWS,TAB_MINE};
        int count = keys.length;
        for(int i = 0; i < count; i++) {
            itemFragment = fragmentManager.findFragmentByTag(keys[i]+"");
            if(null != itemFragment){
                if(keys[i] == showPosition){
                    transaction.show(itemFragment);
                }else{
                    transaction.hide(itemFragment);
                }
            }
        }
        transaction.commitAllowingStateLoss();
        if(fragment instanceof MainStatusFragment){
            MainStatusFragment mainStatusFragment = (MainStatusFragment) fragment;
            mainStatusFragment.refreash();
        }else if(fragment instanceof MainHaveFragment){
            MainHaveFragment mainHaveFragment = (MainHaveFragment) fragment;
            mainHaveFragment.refreash();
        }
        return fragment;
    }
}
